<?php
/**
 * Copyright (c) 2009 Nicholas Aiello  <nikko.aiello@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

require 'factory.class.php';
require 'patterns_factory.class.php';
require 'sections_factory.class.php';

require SITE_ROOT . 'lib/MyMin.php';
require SITE_ROOT . 'includes/common.php';

class Optimizer {
	
	public $pf;
	public $sf;
	
	public function __construct() {
		$this->pf = new PatternsFactory();
		$this->sf = new SectionsFactory();
	}
	
	public function execute(&$pre, &$post, $minify=false, $callback=null) {
		$patterns =& $this->pf->getPatterns();
		$sections =& $this->sf->getSections();
		
		debug($patterns);
		debug($sections);
		
		// iterate through all of the scripts
		for ($i = 0, $leni = count($pre); $i < $leni; ++$i) {
			// get the script
			$script = $pre[$i];
			foreach ($sections as $s_label => $section) {
				// see if the section needs to be targeted
				if ($section->getTarget()) {
					$s_matches = preg_match_all($section->getTarget(), $script, $m) ? $m[0] : array();
				}
				else {
					$s_matches = array($script);
				}
				
				debug($s_matches);
				
				// iterate through each section target match
				for ($j = 0, $lenj = count($s_matches); $j < $lenj; ++$j) {
					// copy the section
					$s_match = $s_opt = $s_matches[$j];
					// get the patterns for the section
					$ps = $section->getPatterns();
					// iterate through each pattern the section supports
					for ($k = 0, $lenk = count($ps); $k < $lenk; ++$k) {
						$p = $patterns[$ps[$k]];
						debug($p->getLabel() . ' - ' . $p->getPattern() . ' - ' . $p->getOptimized());
						
						// if the pattern is found in the section, optimize it
						if (@preg_match_all($p->getPattern(), $s_opt, $p_matches)) {
							$s_opt = $p->getCallbackFunction() ?
								preg_replace_callback($p->getPattern(), create_function('$match',$p->getCallbackFunction()), $s_opt) : preg_replace($p->getPattern(), $p->getOptimized(), $s_opt);
							
							debug($s_opt);
						}
					}
					$script =& str_replace($s_match, $s_opt, $script);
				}
			}
			$post[$i] = $minify ? new MyMin($script, false) : $script;
		}
	}
	
}

?>